import React from 'react';
import {
  UserOutlined, 
  LogoutOutlined, 
  SettingOutlined,
  PieChartOutlined, 
  DesktopOutlined,
  UserSwitchOutlined,
  MenuOutlined,
  UsergroupAddOutlined,
  SnippetsOutlined,
  ShoppingCartOutlined,
  ShoppingOutlined,
  CompassOutlined
} from '@ant-design/icons';
import { message } from 'antd';
import useStore from '../store';

// 下拉菜单配置
export const headerDropdownMenuItems = [
  {
    key: '1',
    label: '个人信息',
    icon: React.createElement(UserOutlined),
    onClick: () => {
      message.info('查看个人信息');
      // 实际项目中这里应该跳转到个人信息页面
      // router.push('/user/profile');
    }
  },
  {
    key: '2',
    label: '设置',
    icon: React.createElement(SettingOutlined),
    onClick: () => {
      message.info('打开设置');
      // 实际项目中这里应该跳转到设置页面
      // router.push('/settings');
    }
  },
  {
    key: '3',
    type: 'divider' as const
  },
  {
    key: '4',
    label: '登出',
    icon: React.createElement(LogoutOutlined),
    danger: true,
    onClick: () => {
      // 1. 显示登出成功提示
      message.success('登出成功');
      
      // 2. 清除用户状态
      const store = useStore.getState();
      store.setUserInfo({ desc: '', imgSrc: '' });
      
      // 3. 清除本地存储中的token（如果有）
      localStorage.removeItem('userToken');
      sessionStorage.removeItem('userToken');
      
      // 4. 由于这是配置文件，不直接处理路由跳转
      // 实际的页面跳转需要在组件中实现
    }
  }
];

// 定义菜单项类型
export interface MenuItem {
  key: string;
  label: string;
  icon?: React.ReactNode;
  route?: string;
  parentKey?: string;
  order?: number;
  children?: MenuItem[];
}

// 菜单字典表 - 包含所有可配置的菜单项
export const menuDictionary: Record<string, MenuItem> = {
  // 控制台模块
  'dashboard': {
    key: 'dashboard',
    label: '控制台',
    icon: React.createElement(PieChartOutlined),
    route: '/dashboard',
    order: 1
  },
  
  // 系统管理模块
  'system': {
    key: 'system',
    label: '系统管理',
    icon: React.createElement(DesktopOutlined),
    order: 2
  },
  'system-user': {
    key: 'system-user',
    label: '用户管理',
    icon: React.createElement(UserSwitchOutlined),
    route: '/system/user',
    parentKey: 'system',
    order: 1
  },
  'system-menu': {
    key: 'system-menu',
    label: '菜单管理',
    icon: React.createElement(MenuOutlined),
    route: '/system/menu',
    parentKey: 'system',
    order: 2
  },
  'system-role': {
    key: 'system-role',
    label: '角色管理',
    icon: React.createElement(UsergroupAddOutlined),
    route: '/system/role',
    parentKey: 'system',
    order: 3
  },
  'system-dept': {
    key: 'system-dept',
    label: '部门管理',
    icon: React.createElement(SnippetsOutlined),
    route: '/system/dept',
    parentKey: 'system',
    order: 4
  },
  
  // 订单管理模块
  'order': {
    key: 'order',
    label: '订单管理',
    icon: React.createElement(ShoppingCartOutlined),
    order: 3
  },
  'order-list': {
    key: 'order-list',
    label: '订单列表',
    icon: React.createElement(ShoppingOutlined),
    route: '/order/list',
    parentKey: 'order',
    order: 1
  },
  'order-aggregate': {
    key: 'order-aggregate',
    label: '订单聚合',
    icon: React.createElement(PieChartOutlined),
    route: '/order/aggregate',
    parentKey: 'order',
    order: 2
  },
  'order-track': {
    key: 'order-track',
    label: '订单追踪',
    icon: React.createElement(CompassOutlined),
    route: '/order/track',
    parentKey: 'order',
    order: 3
  }
};

// 模拟接口获取菜单数据的方法
export const fetchMenuDataFromApi = async (): Promise<string[]> => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  
  // 模拟返回用户有权限的菜单key列表
  // 实际项目中应该根据用户权限返回对应的菜单列表
  return [
    'dashboard',
    'system',
    'system-user',
    'system-menu',
    'system-role',
    'system-dept',
    'order',
    'order-list',
    'order-aggregate',
    'order-track'
  ];
};

// 根据菜单key列表和字典表构建菜单树结构
export const buildMenuTree = (menuKeys: string[]): MenuItem[] => {
  // 过滤出有权限的菜单项
  const authorizedItems = menuKeys
    .map(key => menuDictionary[key])
    .filter(item => item) as MenuItem[];
  
  // 按order排序
  authorizedItems.sort((a, b) => (a.order || 999) - (b.order || 999));
  
  // 构建菜单树
  const menuTree: MenuItem[] = [];
  const menuMap: Record<string, MenuItem> = {};
  
  // 首先创建所有菜单项的副本
  authorizedItems.forEach(item => {
    menuMap[item.key] = { ...item };
  });
  
  // 构建父子关系
  authorizedItems.forEach(item => {
    if (item.parentKey && menuMap[item.parentKey]) {
      // 如果是子菜单项，添加到父菜单
      if (!menuMap[item.parentKey].children) {
        menuMap[item.parentKey].children = [];
      }
      // 检查是否已存在，避免重复添加
      if (!menuMap[item.parentKey].children?.find(child => child.key === item.key)) {
        menuMap[item.parentKey].children?.push(menuMap[item.key]);
      }
    } else if (!item.parentKey) {
      // 如果是顶级菜单，添加到菜单树
      menuTree.push(menuMap[item.key]);
    }
  });
  
  // 对子菜单按order排序
  menuTree.forEach(parent => {
    if (parent.children) {
      parent.children.sort((a, b) => (a.order || 999) - (b.order || 999));
    }
  });
  
  return menuTree;
};